import requests
from bs4 import BeautifulSoup
import sqlite3



base_url = 'https://jinjier.art/3'

response = requests.get(base_url)
soup = BeautifulSoup(response.text, 'html.parser')


conn = sqlite3.connect('content.db')
cursor = conn.cursor()

# 创建表格，包含两个列: one_for_strong 和 one_for_br
cursor.execute('''
CREATE TABLE IF NOT EXISTS content (
    one_for_strong TEXT,
    one_for_br TEXT
)
''')


# 查找所有 <p> 标签
for p in soup.find_all('p'):
    # 提取 <strong> 标签包围的文本
    strong_tag = p.find('strong')
    if strong_tag:
        strong_text = strong_tag.get_text()
        # 替换第一个空格之前的所有字符为 #
        strong_text = '#' + strong_text.split(' ', 1)[-1] if ' ' in strong_text else '#' + strong_text
    else:
        strong_text = None

    # 提取最后一个 <br> 标签后的文本
    br_tag = p.find_all('br')
    if br_tag:
        last_br = br_tag[-1].find_next_sibling(text=True)
        if last_br:
            br_text = last_br.strip()
        else:
            br_text = None
    else:
        br_text = None

    # 将提取到的内容插入数据库
    cursor.execute('''
    INSERT INTO content (one_for_strong, one_for_br)
    VALUES (?, ?)
    ''', (strong_text, br_text))

# 提交事务并关闭连接
conn.commit()
conn.close()

print("数据已成功存入 SQLite 数据库")
